package com.chenfan.mcn.controller;

import com.alibaba.fastjson.JSONObject;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.extension.Res;
import com.chenfan.mcn.model.dto.AeDetailDTO;
import com.chenfan.mcn.model.dto.AeRoyaltiesDTO;
import com.chenfan.mcn.model.dto.AeRoyaltiesExportDTO;
import com.chenfan.mcn.model.dto.AeRoyaltiesUpdateDTO;
import com.chenfan.mcn.service.AeExcutorConfirmService;
import com.chenfan.mcn.service.AeRoyaltiesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * AE提成单
 * @Author chenguopeng
 * @Date 2021/6/7 10:26
 */
@Slf4j
@RestController
@RequestMapping("/aeRoyalties")
public class AeRoyaltiesController {

    @Autowired
    private AeRoyaltiesService aeRoyaltiesService;

    @Autowired
    private AeExcutorConfirmService aeExcutorConfirmService;

    /**
     * 列表查询/弹窗查询
     *
     * @param aeRoyaltiesDTO
     * @return
     */
    @PostMapping("/v1/page")
    public Res<JSONObject> getPage(@RequestBody AeRoyaltiesDTO aeRoyaltiesDTO) {
        return Res.ok(aeRoyaltiesService.getPageView(aeRoyaltiesDTO));
    }

    /**
     * 确认详情列表
     *
     * @param aeDetailDTO
     * @return
     */
    @PostMapping("/v1/aeDetail")
    public Res<JSONObject> getAeDetail(@RequestBody AeDetailDTO aeDetailDTO) {
        return Res.ok(aeRoyaltiesService.getAeDetail(aeDetailDTO));
    }

    /**
     * 修改AE提成单
     *
     * @param
     * @return
     */
    @PostMapping("/v1/update")
    public Res<JSONObject> update(@RequestBody AeRoyaltiesUpdateDTO aeRoyaltiesUpdateDTO) {
        aeRoyaltiesService.update(aeRoyaltiesUpdateDTO);
        return Res.ok();
    }

    /**
     * 批量删除AE提成单
     *
     * @param ids
     * @return
     */
    @PostMapping("/v1/batchDelete")
    public Res<JSONObject> batchDelete(@RequestBody List<Long> ids) {
        aeRoyaltiesService.batchDelete(ids);
        return Res.ok();
    }

    /**
     * AE提成详情/弹窗导出excel
     *
     * @param aeRoyaltiesExportDTO
     * @param response
     * @return
     */
    @PostMapping("/v1/exportExcel")
    public Res<Object> exportExcel(@RequestBody AeRoyaltiesExportDTO aeRoyaltiesExportDTO, HttpServletResponse response) {
        try {
            aeRoyaltiesService.exportExcel(aeRoyaltiesExportDTO, response);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Res.failed(McnErrorCode.EXPORT_EXCEPTION);
        }
        return Res.ok();
    }

    /**
     * 提成单详情发送提成单按钮
     *
     * @param id
     * @return
     */
    @PostMapping("/v1/sendCommission/{id}")
    public Res<Object> sendCommission(@PathVariable Long id) {
        aeExcutorConfirmService.sendCommission(id);
        return Res.ok();
    }


}
